﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using Zvezda.FSO2.DB;

namespace FSO.Reports
{
    /// <summary>
    /// Компонент для отображения параметров отчета "Полученные средства по исходящим счетам"
    /// </summary>
    public partial class UserControlRecievedMeansOutInvoiceParams : UserControl, IReportParamList
    {
        public UserControlRecievedMeansOutInvoiceParams()
        {
            InitializeComponent();
        }

        #region IReportParamList Members

        public Microsoft.Reporting.WinForms.ReportParameter[] Parameters
        {
            get 
            {
                List<ReportParameter> _listParams = new List<ReportParameter>(7);

                _listParams.Add(new ReportParameter("p_date_from", userControlDatePeriodSelector1.DateFrom.ToShortDateString(), false));

                _listParams.Add(new ReportParameter("p_date_to", userControlDatePeriodSelector1.DateTo.ToShortDateString(), false));

                string sTypeOfActivityValue = null;
                string sTypeOfActivityText = null;
                if (!userControlTypeOfActivitySelector1.IsNull)
                {
                    sTypeOfActivityValue = userControlTypeOfActivitySelector1.SelectedValue;
                    sTypeOfActivityText = userControlTypeOfActivitySelector1.SelectedText;
                }
                _listParams.Add(new ReportParameter("p_nsi_type_of_activity_id", sTypeOfActivityValue, false));
                _listParams.Add(new ReportParameter("v_nsi_type_of_activity", sTypeOfActivityText, false));

                string sContractValue = null;
                string sContractNumber = null;
                if (!userControlContractSelector1.IsNull)
                {
                    sContractValue = userControlContractSelector1.SelectedRow.ID.ToString();
                    sContractNumber = userControlContractSelector1.SelectedRow.NUMBER;
                }
                _listParams.Add(new ReportParameter("p_contract_id", sContractValue, false));
                _listParams.Add(new ReportParameter("v_contract_number", sContractNumber, false));

                string sNsiOrganization = null;
                string sNsiOrganiztionName = null;
                if (!userControlOrganizationSelector1.IsNull)
                {
                    sNsiOrganization = userControlOrganizationSelector1.SelectedRow.ID.ToString();
                    sNsiOrganiztionName = userControlOrganizationSelector1.SelectedRow.SHORT_NAME;
                }
                _listParams.Add(new ReportParameter("p_nsi_organization_id", sNsiOrganization, false));
                _listParams.Add(new ReportParameter("v_organization", sNsiOrganiztionName, false));

                string sPaymentIn = null;
                if (radCheckBoxPaymentInIsNull.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On && _SelectedPaymentInID > 0)
                {
                    sPaymentIn = _SelectedPaymentInID.ToString();
                }
                _listParams.Add(new ReportParameter("p_payment_in_id", sPaymentIn, false));

                string InvoiceOutID = null;
                if (radCheckBoxInvoiceOutIsNull.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On && _SelectedInvoiceOut > 0)
                {
                    InvoiceOutID = _SelectedInvoiceOut.ToString();
                }
                _listParams.Add(new ReportParameter("p_invoice_out_id", InvoiceOutID, false));

                string sSumFrom = null;
                string sSumTo = null;

                if (!userControlSelectSum1.IsNull)
                {
                    sSumFrom = userControlSelectSum1.FromSum.ToString();
                    sSumTo = userControlSelectSum1.ToSum.ToString();
                }
                _listParams.Add(new ReportParameter("p_sum_from", sSumFrom, false));
                _listParams.Add(new ReportParameter("p_sum_to", sSumTo, false));

                return _listParams.ToArray();
            }
        }

        #endregion

        /// <summary>
        /// Загружает данные из БД
        /// </summary>
        private void UserControlRecievedMeansParams_Load(object sender, EventArgs e)
        {
        }

        private void radCheckBoxPaymentInIsNull_ToggleStateChanged(object sender, Telerik.WinControls.UI.StateChangedEventArgs args)
        {
            if (args.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On)
            {
                radLabelPaymentInNumber.Enabled = true;
                radButtonSelectPaymentIn.Enabled = true;
            }
            else
            {
                radLabelPaymentInNumber.Enabled = false;
                radButtonSelectPaymentIn.Enabled = false;
            }
        }
        /// <summary>
        /// Выбранный входящий платеж
        /// </summary>
        private int _SelectedPaymentInID = -1;
        /// <summary>
        /// Открывает окно выбора входящего платежа
        /// </summary>
        private void radButtonSelectPaymentIn_Click(object sender, EventArgs e)
        {
            DialogFormSelectObject dlgFrmSlectObject = new DialogFormSelectObject(ObjctTypeEnum.PAYMENT_IN);
            if (dlgFrmSlectObject.ShowDialog() == DialogResult.OK)
            {
                DataSetFSO.PAYMENT_INRow drPaymentIn = dlgFrmSlectObject.SelectedRow as DataSetFSO.PAYMENT_INRow;
                if (drPaymentIn != null)
                {
                    _SelectedPaymentInID = drPaymentIn.ID;
                    radLabelPaymentInNumber.Text = drPaymentIn.NUMBER;
                }
            }
        }
        /// <summary>
        /// ID выбранного исходящего счета
        /// </summary>
        private int _SelectedInvoiceOut = -1;
        /// <summary>
        /// Открывает окно выбора исходящего счета
        /// </summary>
        private void radButtonSelectInvoiceOut_Click(object sender, EventArgs e)
        {
            DialogFormSelectObject dlgFrmSlectObject = new DialogFormSelectObject(ObjctTypeEnum.INVOICE_OUT);
            if (dlgFrmSlectObject.ShowDialog() == DialogResult.OK)
            {
                DataSetFSO.INVOICE_OUTRow drInvoiceOut = dlgFrmSlectObject.SelectedRow as DataSetFSO.INVOICE_OUTRow;
                if (drInvoiceOut != null)
                {
                    _SelectedInvoiceOut = drInvoiceOut.ID;
                    radLabelInvoiceOutNumber.Text = drInvoiceOut.NUMBER;
                }
            }
        }

        private void radCheckBoxInvoiceOutIsNull_ToggleStateChanged(object sender, Telerik.WinControls.UI.StateChangedEventArgs args)
        {
            if (args.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On)
            {
                radButtonSelectInvoiceOut.Enabled = true;
            }
            else
            {
                radButtonSelectInvoiceOut.Enabled = false;
            }
        }
    }
}
